/**
 * Copyright &copy; 2016-2017 <a href="https://www.lczyfz.com">联创智云</a> All rights reserved.
 */
package com.lczyfz.campusinfo.modules.wechatmsg.service;

import java.net.ServerSocket;
import java.util.List;

import com.lczyfz.campusinfo.common.utils.StringUtils;
import com.lczyfz.campusinfo.modules.user.dao.CampusinfoUserDao;
import com.lczyfz.campusinfo.modules.user.utils.ServerResponse;
import com.lczyfz.campusinfo.modules.wechat.service.WeChatService;
import com.lczyfz.campusinfo.modules.wechat.utils.WeChatUtils;
import com.lczyfz.campusinfo.modules.wechatmsg.dao.WeixinMsgRecevierDao;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinMsgRecevier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lczyfz.campusinfo.common.persistence.Page;
import com.lczyfz.campusinfo.common.service.CrudService;
import com.lczyfz.campusinfo.modules.wechatmsg.entity.WeixinMsg;
import com.lczyfz.campusinfo.modules.wechatmsg.dao.WeixinMsgDao;

/**
 * 微信消息Service
 * @author 陈维乾
 * @version 2018-05-07
 */
@Service
@Transactional(readOnly = true)
public class WeixinMsgService extends CrudService<WeixinMsgDao, WeixinMsg> {

    @Autowired
    private WeChatService weChatService;

	@Autowired
	private WeixinMsgRecevierDao weixinMsgRecevierDao;

	@Autowired
	private CampusinfoUserDao campusinfoUserDao;

	public WeixinMsg get(String id) {
		return super.get(id);
	}
	
	public List<WeixinMsg> findList(WeixinMsg weixinMsg) {
		return super.findList(weixinMsg);
	}
	
	public Page<WeixinMsg> findPage(Page<WeixinMsg> page, WeixinMsg weixinMsg) {
		weixinMsg.setPage(page);
		List<WeixinMsg> weixinMsgs = dao.findList(weixinMsg);
		for(WeixinMsg weixinMsg1 : weixinMsgs){
			String id = weixinMsg1.getId();
			//获取id对应得接收者List<String>

			List<String> strings = weixinMsgRecevierDao.getOpenIdNameByMsgId(id);
			//遍历list 获取每个对应得userName 然后赋值给weixinMsgs

			StringBuilder names = new StringBuilder();
			for(String openId:strings) {
				String name = campusinfoUserDao.getUserNameByOpenId(openId);
				names.append(name + "、");

			}
//			System.err.println(names.toString());
			weixinMsg1.setReceviers(names.toString());
		}
		page.setList(weixinMsgs);
		return page;

	}
	
	@Transactional(readOnly = false)
	public void save(WeixinMsg weixinMsg) {
		if (weixinMsg.getIsNewRecord()){
			weixinMsg.preInsert();
			dao.insert(weixinMsg);
			for(String msgUser:weixinMsg.getRecevier()){
				WeixinMsgRecevier weixinMsgRecevier = new WeixinMsgRecevier();
				weixinMsgRecevier.preInsert();
				weixinMsgRecevier.setMsgId(weixinMsg.getId());
				weixinMsgRecevier.setOpenId(msgUser);
				weixinMsgRecevierDao.insert(weixinMsgRecevier);
			}
		}else{
			weixinMsg.preUpdate();
			dao.update(weixinMsg);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(WeixinMsg weixinMsg) {
		super.delete(weixinMsg);
	}

	//群发用户
	public ServerResponse sendMsgs(WeixinMsg weixinMsg){

	    String user[] = weixinMsg.getRecevier();
	    StringBuilder users = new StringBuilder();
		String users1 ;
		if(user == null || user.length == 0 ){
			return ServerResponse.createByErrorMessage("传入的openIds不能为空");
		}

		if(user.length <2){
			for(String str:user){
				users =  users.append("\""+str+"\",");
				users =  users.append("\""+str+"\"");
			}
			users1 = users.toString();
		}else {
			for(String str:user){
				users.append("\""+str+"\",");
			}
			users1 = users.substring(0,users.length()-1);

		}
        logger.debug("users:"+users1);
	    String content = weixinMsg.getContent();
        ServerResponse serverResponse = weChatService.sendTextMsgToMass(users1,content);
		return serverResponse;
	}
}